package review.system.action;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import review.config.Constants;
import review.entity.Company;
import review.system.service.CompanyService;

@Controller
@RequestMapping("/syatem")
@Api(tags = { "单位操作接口" })
public class CompanyAction {

  @Autowired
  private CompanyService companyService;

  @ApiOperation(value = "添加单位", notes = "", httpMethod = "POST")
  @RequestMapping(value = "/add_company", produces = "application/json;charset=UTF-8")
  @ResponseBody
  public Object add(Company co) {
    JSONObject obj = new JSONObject();
    try {
      companyService.addCompany(co);
      obj.put(Constants.SUCCESS, true);
      obj.put(Constants.DATA, co);
    } catch (Exception e) {
      obj.put(Constants.MESSAGE, e.getMessage());
      obj.put(Constants.SUCCESS, false);
    }
    return obj;
  }

  @ApiOperation(value = "分页查询单位", notes = "", httpMethod = "GET")
  @RequestMapping(value = "/query_company", produces = "application/json;charset=UTF-8")
  @ResponseBody
  public Object query(Company co, @RequestParam(defaultValue = "1") int page,
      @RequestParam(defaultValue = "10") int size) {
    JSONObject obj = new JSONObject();
    try {
      Page<Company> depts = companyService.findAll(page, size, co);
      obj.put(Constants.SUCCESS, true);
      obj.put(Constants.DATA, depts);
    } catch (Exception e) {
      obj.put(Constants.MESSAGE, e.getMessage());
      obj.put(Constants.SUCCESS, false);
    }
    return obj;
  }

  @ApiOperation(value = "删除单位", notes = "", httpMethod = "POST")
  @RequestMapping(value = "/delete_company", produces = "application/json;charset=UTF-8")
  @ResponseBody
  public Object delete(Company co) {
    JSONObject obj = new JSONObject();
    try {
      companyService.delectCompany(co);
      obj.put(Constants.SUCCESS, true);
      obj.put(Constants.DATA, co);
    } catch (Exception e) {
      obj.put(Constants.MESSAGE, e.getMessage());
      obj.put(Constants.SUCCESS, false);
    }
    return obj;
  }

  @ApiOperation(value = "修改单位", notes = "", httpMethod = "POST")
  @RequestMapping(value = "/update_company", produces = "application/json;charset=UTF-8")
  @ResponseBody
  public Object update(Company co) {
    JSONObject obj = new JSONObject();
    try {
      companyService.updateCompany(co);
      obj.put(Constants.SUCCESS, true);
      obj.put(Constants.DATA, co);
    } catch (Exception e) {
      obj.put(Constants.MESSAGE, e.getMessage());
      obj.put(Constants.SUCCESS, false);
    }
    return obj;
  }

}
